package com.example.tuomin.aop;

import gov.jyy.xzxk.xksq.common.desensitization.DesensitizationContext;
import gov.jyy.xzxk.xksq.common.desensitization.EnableDesensitization;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @className: DesensitizationAspect
 * @description: TODO 类描述
 * @author: liangshijie
 * @date: 2023/4/26
 **/
@Aspect
@Component
public class DesensitizationAspect {


    @Pointcut("@annotation(gov.jyy.xzxk.xksq.common.desensitization.EnableDesensitization)")
    public void enableDesensitization() {}

    @Around("enableDesensitization()")
    public Object desensitizeData(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Object result = joinPoint.proceed();
        EnableDesensitization desensitization = method.getAnnotation(EnableDesensitization.class);
        if(desensitization != null) {
            DesensitizationContext.setValue(result);
        }
        return result;
    }

}